package com.example.software.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class material_Util {
    //阿里域名

    public static final String ALI_DOMAIN = "https://czh0123-text.oss-cn-hangzhouzhou.aliyuncs.com/";


    public static String uploadImage(MultipartFile file) throws IOException {
        //生成文件名

        String originalFilename =    file.getOriginalFilename();//原来的图片
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
//        String uuid = "w1122";
        //为了不重复一直上传音频
        String filename = uuid+ext;

        //      //汪xy
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tPadF1rFQBECwN1HFuN";
        String accessKeySecret = "P0hGcIAWABdLNgjAnjc1FlLHdhqJEj";
        String bucketName = "wxyyw";

        //公司
//        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//        String accessKeyId = "LTAI5tR8t41TiXSiGpVRa6Fr";
//        String accessKeySecret = "nglf9N9TwXJYSgMjykgwQKarFJK2yv";
//        String bucketName = "fast-coffee";

        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(
                bucketName, //存储空间名称
                filename, //文件名称
                file.getInputStream()//文件输入流
//                (ObjectMetadata) ContentDisposition.attachment()
        );

        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + filename;
        ossClient.shutdown();

        return url;
//        return ALI_DOMAIN+filename;
    }


    public static String uploadImage2(File file) throws IOException {
        //生成文件名

        String originalFilename =    file.getName();;//原来的图片
        // String originalFilename =   "tts_text.mp3";
        String ext = "." + FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
//        String uuid = "w1122";
        //为了不重复一直上传音频
        String filename = uuid+ext;
        System.out.println("filename"+filename);

        //地域节点 吴永杰oss
//        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//        String accessKeyId = "LTAI5tARrUEbvP5pKtoWFKvT";
//        String accessKeySecret = "jXmFaGnQD53Xj5MJ34u6iuNio69oVD";
//        String bucketName = "tiebingice";

//        //郑嘉俊oss
//        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
//        String accessKeyId = "LTAI5t6Yxfn6u2az6xpUGqJu";
//        String accessKeySecret = "ZafmIIfoozoggb8erlxPjGAQyqPUWo";
//        String bucketName = "sky-itcast-jiajun";

        //      //汪xy
        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tPadF1rFQBECwN1HFuN";
        String accessKeySecret = "P0hGcIAWABdLNgjAnjc1FlLHdhqJEj";
        String bucketName = "wxyyw";


        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(
                bucketName, //存储空间名称
                filename, //文件名称
                file//文件输入流
//                (ObjectMetadata) ContentDisposition.attachment()
        );

        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + filename;
        ossClient.shutdown();

        return url;
//        return ALI_DOMAIN+filename;


    }
}
